<link rel="stylesheet" href="${request.contextPath}/static/oms/default/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<link href="${request.contextPath}/static/oms/iframe/plugins/bootstrap-icon-picker/css/icon-picker.css"  rel="stylesheet" type="text/css" />
<div class="row">
	<div class="col-md-12">
		<div class="box-body  no-padding">
			<form id="update-from" class="form-horizontal" action="${request.contextPath}/notepad/favorite/category/update" method="post" onsubmit="return false">
				<input type="hidden" class="form-control" name="id"  id="favoriteCategoryId"  value="${favoriteCategory.id}" >
				<div class="form-group">
					<label for="favoriteCategoryName" class="col-sm-2 control-label">名称</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="name"  id="favoriteCategoryName"  value="${favoriteCategory.name}" placeholder="输入名称...">
					</div>
					<div class="form-group">
						<label for="favoriteCategoryUsername" class="col-sm-1 control-label">用户名</label>
						<div class="col-sm-4">
							<input type="text" class="form-control" name="username" id="favoriteCategoryUsername"  value="${favoriteCategory.username}" placeholder="输入用户名...">
						</div>
					</div>

				</div>
				<div class="form-group">
					<label id="favoriteCategoryParentIdLabel" for="favoriteCategoryParentId" class="col-sm-2  control-label">上级资源</label>
					<div class="col-sm-9">
						<div class="input-group">
							<input type="hidden" class="form-control" id="favoriteCategoryParentId" name="parentId"  value="${favoriteCategory.parentId}" >
							<input type="text" class="form-control"  id="favoriteCategoryParentName" value="" placeholder="选择上级资源...">
							<span class="input-group-addon"><i class="glyphicon glyphicon-remove" style="color: #FF0000;" id="favoriteCategoryParentIdClear"></i></span>
							<div id="favoriteCategoryContent" class="favoriteCategoryContent" style="position: absolute; left: 0; top: 25px; display: none;z-index: 999999;">
								<ul id="favoriteCategoryParentTree" class="ztree" style="margin-top: 10px;
																border: 1px solid #d6e0df;
																background: #fbfbfb;
																width: 220px;
																height: 360px;
																overflow-y: scroll;
																overflow-x: auto; -moz-user-select: none;">
								</ul>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label id="favoriteCategorySortOrderLabel" for="favoriteCategorySortOrder" class="col-sm-2  control-label">排序号</label>
					<div class="col-sm-2">
						<input class="form-control" name="sortOrder" id="favoriteCategorySortOrder" type="number" value="${favoriteCategory.sortOrder}" placeholder="输入排序号...">
					</div>
					<label id="favoriteCategoryStatusLabel" for="favoriteCategoryStatus" class="col-sm-1  control-label">状态</label>
					<div class="col-sm-2">
						<select class="form-control" id="favoriteCategoryStatus"  name="status"  value="" style="width: 100%;">
						</select>
					</div>
					<label id="favoriteCategoryAccessLevelLabel" for="favoriteCategoryAccessLevel" class="col-sm-2  control-label">访问级别</label>
					<div class="col-sm-2">
						<select class="form-control" id="favoriteCategoryAccessLevel" name="accessLevel"  value=""  >
						</select>
					</div>
				</div>
				<div class="form-group">
					<label id="favoriteCategoryDescriptionLabel" class="col-sm-2 control-label" >描述</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" name="description" id="favoriteCategoryDescription"  value="${favoriteCategory.description}" placeholder="输入描述...">
					</div>
				</div>
				<div class="form-group">
					<label id="favoriteCategoryRemarkLabel" class="col-sm-2 control-label">备注</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" name="remark" id="favoriteCategoryRemark"  value="${favoriteCategory.remark}"  placeholder="输入备注...">
					</div>
				</div>
				<div class="error-message text-center" >
					<span class="error-icon"></span>
					<span id="error-msg" class="error-msg" ></span>
				</div>
				<div class="box-footer">
					<div class="pull-right">
						<button type="submit" class="btn btn-primary btn-sm" ><i class="fa fa-save"></i> 保存</button>
						<button type="button" class="btn btn-default btn-sm" id="close" data-dismiss="modal"><i class="fa fa-close"></i>关闭</button>
					</div>
				</div>
			</form>
		</div>

	</div>
</div>
<script type="text/javascript" src="${request.contextPath}/static/oms/default/plugins/zTree/jquery.ztree.all.js"></script>
<script type="text/javascript" src="${request.contextPath}/static/oms/iframe/plugins/bootstrap-icon-picker/js/iconPicker.js"></script>

<script type="text/javascript">
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            }
        },
        async: {
            enable: true,
            url:"/notepad/favorite/category/tree",
            dataFilter:function (treeId, parentNode, responseData) {
                if (responseData) {
                    for(var i =0; i < responseData.length; i++) {
                        responseData[i].url = null;
                    }
                }
                return responseData;
            }
        },
        callback:{
            onAsyncSuccess:function (event, treeId) {
                var node = $.fn.zTree.getZTreeObj(treeId).getNodeByParam("id", $("#favoriteCategoryParentId").val());
                if(node){
                    $("#favoriteCategoryParentName").val(node.name);
                }
            },
            onClick :function (event, treeId, treeNode) {
				$("#favoriteCategoryParentId").val(treeNode.id);
				$("#favoriteCategoryParentName").val(treeNode.name);
                $('#favoriteCategoryContent').hide();
            }
        }
    };

	$(function() {

        $("#favoriteCategoryAccessLevel").select2({
			data: AccessLevelStore,
            language: "zh-CN",
            theme: "bootstrap"
		});
        $("#favoriteCategoryAccessLevel").select2('val','${favoriteCategory.accessLevel}');


		$("#favoriteCategoryStatus").select2({
			data: StatusStore,
			language: "zh-CN",
            theme: "bootstrap"
		});
        $("#favoriteCategoryStatus").select2("val",'${favoriteCategory.status}');

        var $favoriteCategoryUsername = $("#favoriteCategoryUsername");
        if($favoriteCategoryUsername.val()){
            setting.async.url="/notepad/favorite/category/tree?username="+$favoriteCategoryUsername.val();
            $.fn.zTree.init($('#favoriteCategoryParentTree'), setting);
		}
        $favoriteCategoryUsername.on('blur',function () {
            var $this = $(this);
            if($this.val()){
                setting.async.url="/notepad/favorite/category/tree?username="+$this.val();
                $.fn.zTree.init($('#favoriteCategoryParentTree'), setting);
            }
        });

        $("#favoriteCategoryParentName").on('click',function () {
            var $favoriteCategoryContent = $('#favoriteCategoryContent');
            $favoriteCategoryContent.css("display")==='none'?$favoriteCategoryContent.show():$favoriteCategoryContent.hide();
        });
        $("#favoriteCategoryContent").mouseleave(function () {
            $('#favoriteCategoryContent').hide();
        });

        $("#favoriteCategoryParentIdClear").on('click',function () {
            $("#favoriteCategoryParentId").val("");
            $("#favoriteCategoryParentName").val("");
        });



        function ajaxSubmit(form) {

            var $form = $(form);

            if ($form.length === 1 && $form.validate()) {
                $.post(form.action, $form.serialize(),
                    function(result) {
                        if (result.success) {
                            $("#lgModal").modal('hide');
                            alertMsg(result.message,"success");
                            window.location.reload();
                        } else {
                            $form.find(".error-msg").show();
                            $form.find(".error-msg").html(result.message);
                        }
                    }, 'json');
            }
        }

        var $updateForm = $("#update-from");
        $updateForm.validate({
            rules: {
                name: {
                    required: true
                },
                permission: {
                    required: true
                },
                sortOrder: {
                    required: true
                }
            },
            messages: {
                name:{
                    required:"名称不能为空！"
                },
                permission: {
                    required: "权限不能为空！"
                },
                sortOrder: {
                    required: "排序号不能为空！"
                }
            },
            submitHandler:ajaxSubmit,
            errorLabelContainer:".error-msg",
            wrapper:"li",
            showErrors:function(errorMap,errorList) {
                if(errorList.length>0){
                    $updateForm.find(".error-msg").html(errorList[0].message);
                }else{
                    $updateForm.find(".error-msg").html("");
                }
            },
            onkeyup: false
        });
    });
</script>
